// 二次封装axios
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { App } from 'vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'

console.log(
  'import.meta.env.VITE_APP_BASE_API',
  import.meta.env.VITE_APP_BASE_API,
)
const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API, // /api
  timeout: 5000,
})

request.interceptors.request.use(
  (config) => {
    const userStore = useUserStore()

    if (userStore.token) {
      config.headers.token = userStore.token
    }

    return config
  },
  (error) => {
    return Promise.reject(error)
  },
)

request.interceptors.response.use(
  (response) => {
    if (response.status === 200) {
      return Promise.resolve(response.data)
    } else {
      return Promise.reject(response.data)
    }
  },
  (error) => {
    let message = ''
    const status = error.response.status
    switch (status) {
      // 401: 未登录
      // 未登录则跳转登录页面，并携带当前页面的路径
      // 在登录成功后返回当前页面，这一步需要在登录页操作。
      case 401:
        message = '未登录'
        break
      // 403 token过期
      // 登录过期对用户进行提示
      // 清除本地token和清空vuex中token对象
      // 跳转登录页面
      case 403:
        message = '登录过期，请重新登录'
        break
      case 404:
        message = '网络请求不存在'
        break
      case 500:
        message = '服务器出现问题'
        break
      default:
        message = error.response.data.message
        break
    }
    ElMessage({
      type: 'error',
      message,
    })
    return Promise.reject(error)
  },
)
const myGet = (url: any, config: any) => {
  if (!url) throw Error('请核对请求链接是否正确')
  console.log('config', config)
  // const token = window.localStorage.storetoken
  const { params, headers, ...otherConfig } = config
  console.log('params', params)
  const header = Object.assign(headers || {}, {
    // 'Content-Type': 'application/json; charset=utf-8',
    // 'Auth-Token': token,
    // 'Common-Param': getCommonParam()
  })
  return request
    .get(url, {
      params,
      headers: header,
      ...otherConfig,
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
const myPost = (url: any, params: any, config = {}, type = 1) => {
  if (!url) throw Error('请核对请求链接是否正确')
  // const token = window.localStorage.storetoken
  // const {...otherConfig} = config
  let source
  if (type == 1) {
    source = { 'Content-Type': 'application/json; charset=utf-8' } // json 数据
  } else if (type == 2) {
    source = { 'Content-Type': 'multipart/form-data' } // formData表单
  } else {
    source = {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    } // 普通表单
  }
  const header = Object.assign({}, source)
  return request
    .post(url, params, {
      headers: header,
      ...config,
      ...source,
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
const downLoadFile = (url: any, params: any) => {
  if (!url) throw Error('请核对请求链接是否正确')
  /*  const header = Object.assign({}, {
        headers: {
            "Content-Type":"application/json",
        },
    })*/
  return request
    .post(url, params, {
      headers: {
        'Content-Type': 'application/json',
      },
      responseType: 'blob',
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
/*const install = function (app: App) {
    app.protoType.$post = myPost
    app.protoType.$get = myGet
}*/
export default {
  request,
  myGet,
  myPost,
}
